<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 21] The java.awt.image Package</TITLE>
<META NAME="author" CONTENT="David Flanagan">
<META NAME="date" CONTENT="Thu Jul 31 16:15:30 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java in a Nutshell">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">
<script language='JavaScript'>
<!--
if (location.replace) {
 location.replace ('ch21_js.htm');
}
// -->
</script>

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java in a Nutshell" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch20_30.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 21</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch21_02.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<H1 CLASS=chapter><A CLASS="TITLE" NAME="JNUT2-CH-21">21. The java.awt.image Package</A></H1>

<DIV CLASS=htmltoc>

<p>
<b>Contents:</b><br>
<a href='ch21_02.htm'>java.awt.image.ColorModel (JDK 1.0)</a><br>
<a href='ch21_03.htm'>java.awt.image.CropImageFilter (JDK 1.0)</a><br>
<a href='ch21_04.htm'>java.awt.image.DirectColorModel (JDK 1.0)</a><br>
<a href='ch21_05.htm'>java.awt.image.FilteredImageSource (JDK 1.0)</a><br>
<a href='ch21_06.htm'>java.awt.image.ImageConsumer (JDK 1.0)</a><br>
<a href='ch21_07.htm'>java.awt.image.ImageFilter (JDK 1.0)</a><br>
<a href='ch21_08.htm'>java.awt.image.ImageObserver (JDK 1.0)</a><br>
<a href='ch21_09.htm'>java.awt.image.ImageProducer (JDK 1.0)</a><br>
<a href='ch21_10.htm'>java.awt.image.IndexColorModel (JDK 1.0)</a><br>
<a href='ch21_11.htm'>java.awt.image.MemoryImageSource (JDK 1.0)</a><br>
<a href='ch21_12.htm'>java.awt.image.PixelGrabber (JDK 1.0)</a><br>
<a href='ch21_13.htm'>java.awt.image.RGBImageFilter (JDK 1.0)</a><br>
<a href='ch21_14.htm'>java.awt.image.ReplicateScaleFilter (JDK 1.1)</a><br>
<p>
<p>
</DIV>

<P CLASS=para>
<A NAME="CH21.IMAGES-JAVA.1"></A><A NAME="CH21.JAVA.AWT.IMA1"></A>The <tt CLASS=literal>java.awt.image</tt> package is, by any standard, a
confusing one. The purpose of the package is to support
image processing, and the classes in the package provide a
powerful infrastructure for that purpose. (see
<A HREF="ch21_01.htm#JNUT2-CH-21-FIG-1">Figure 21.1</A>.)
Most of the classes are part of the infrastructure, however,
and are not normally used by ordinary applications that have
only simple image manipulation requirements.

<P CLASS=para>
To understand this package, it is first important to note
that the <tt CLASS=literal>Image</tt> class itself is part of the
<tt CLASS=literal>java.awt</tt> package, not the <tt CLASS=literal>java.awt.image</tt>
package. Furthermore, the <tt CLASS=literal>java.awt.image</tt> classes
are not the source of images; they simply serve to
manipulate images that come from somewhere else. The
<tt CLASS=literal>Applet.getImage()</tt> method is perhaps the most common
method for obtaining an image in Java--it downloads the
image from a specified URL.  In a stand-alone application, the
<tt CLASS=literal>URL.getContent()</tt> method can be used to obtain an
<tt CLASS=literal>ImageProducer</tt> object, which can then be passed to
the <tt CLASS=literal>Component.createImage()</tt> method to obtain an
<tt CLASS=literal>Image</tt> object.

<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="JNUT2-CH-21-FIG-1">Figure 21.1: The java.awt.image package</A></h4>


<p>
<img align=middle src="./figs/jn2_2101.gif" alt="[Graphic: Figure 21-1]" width=464 height=323 border=0>

</DIV>

<P CLASS=para>
The <tt CLASS=literal>ImageProducer</tt> interface is one you'll encounter
frequently in <tt CLASS=literal>java.awt.image</tt>. It represents an
image source. If you've created an <tt CLASS=literal>Image</tt> object
with <tt CLASS=literal>Applet.getImage()</tt>, you can obtain the
<tt CLASS=literal>ImageProducer</tt> for that <tt CLASS=literal>Image</tt> (which has not
been downloaded yet) with <tt CLASS=literal>Image.getSource()</tt>.
Conversely, given an <tt CLASS=literal>ImageProducer</tt> object, you can
create an <tt CLASS=literal>Image</tt> from it with the
<tt CLASS=literal>createImage()</tt> method of any <tt CLASS=literal>Component</tt> (such
as an <tt CLASS=literal>Applet</tt>). Once you have an
<tt CLASS=literal>ImageProducer</tt> object, you can manipulate it with the
other <tt CLASS=literal>java.awt.image</tt> classes.

<P CLASS=para>
<tt CLASS=literal>FilteredImageSource</tt> is the most important class for
image manipulation. It is itself a type of
<tt CLASS=literal>ImageProducer</tt> that, when created, applies a
specified <tt CLASS=literal>ImageFilter</tt> object to some other specified
<tt CLASS=literal>ImageProducer</tt> object. The
<tt CLASS=literal>FilteredImageSource</tt> thus configured can be used as
an <tt CLASS=literal>ImageProducer</tt> to display a filtered image.
<tt CLASS=literal>CropImageFilter</tt> is a predefined type of
<tt CLASS=literal>ImageFilter</tt> that you can use to extract a specified
rectangle out of a larger image.

<P CLASS=para>
<tt CLASS=literal>RGBImageFilter</tt> is another subclass of
<tt CLASS=literal>ImageFilter</tt> that makes it easy to filter the colors
of an image. To do so, you must subclass
<tt CLASS=literal>RGBImageFilter</tt> and provide the definition of a
single simple method that manipulates the image colors. In
order to manipulate image colors, you will probably need to
be familiar with the <tt CLASS=literal>ColorModel</tt> class and its two
subclasses, <tt CLASS=literal>DirectColorModel</tt> and
<tt CLASS=literal>IndexColorModel</tt>. An instance of <tt CLASS=literal>ColorModel</tt>
or of one of its subclasses converts a pixel value to the
red, green, and blue components of the color it represents.

<P CLASS=para>
Finally, two other classes in the <tt CLASS=literal>java.awt.image</tt> package 
are worth noting. <tt CLASS=literal>MemoryImageSource</tt> is a type of
<tt CLASS=literal>ImageProducer</tt> that generates an image from an array
of bytes or integers in memory. <tt CLASS=literal>PixelGrabber</tt> does the
reverse--it captures pixels from an <tt CLASS=literal>ImageProducer</tt>
and stores them into an array. You can use these classes
separately or together to perform your own custom image
manipulation.

<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JNUT2-CH-21-SECT-1">21.1 java.awt.image.AreaAveragingScaleFilter (JDK 1.1)</A></h2>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-21-SECT-1.1"></A></h3>
</DIV>

<P CLASS=para>
This class implements an <tt CLASS=literal>ImageFilter</tt> that scales an
image to a specified pixel size.  It uses a scaling
algorithm that averages adjacent pixel values when shrinking
an image, which produces relatively smooth scaled images.
Its superclass, <tt CLASS=literal>ReplicateScaleFilter</tt>, implements a
faster, less smooth scaling algorithm.

<P CLASS=para>
The methods of this class are <tt CLASS=literal>ImageConsumer</tt> methods
intended for communication between the image filter and the
<tt CLASS=literal>FilteredImageSource</tt> that uses it.  Applications do
not usually call these methods directly.

<P CLASS=para>
The easiest way to use this filter is to call
<tt CLASS=literal>Image.getScaledInstance()</tt>, specifying an appropriate
hint constant.

<DIV CLASS=screen>
<P>
<PRE>
public class <tt CLASS=literal>AreaAveragingScaleFilter</tt> extends ReplicateScaleFilter {
    // <tt CLASS=literal>Public Constructor</tt>
            public <tt CLASS=literal>AreaAveragingScaleFilter</tt>(int <tt CLASS=literal>width</tt>, int <tt CLASS=literal>height</tt>);
    // <tt CLASS=literal>Public Instance Methods</tt>
            public void <tt CLASS=literal>setHints</tt>(int <tt CLASS=literal>hints</tt>);  //<tt CLASS=literal> Overrides ImageFilter</tt>
            public void <tt CLASS=literal>setPixels</tt>(int <tt CLASS=literal>x</tt>, int <tt CLASS=literal>y</tt>, int <tt CLASS=literal>w</tt>, int <tt CLASS=literal>h</tt>, ColorModel <tt CLASS=literal>model</tt>, byte[] <tt CLASS=literal>pixels</tt>, int <tt CLASS=literal>off</tt>, int <tt CLASS=literal>scansize</tt>);
            public void <tt CLASS=literal>setPixels</tt>'u'//<tt CLASS=literal> Overrides ReplicateScaleFilter</tt>
            public void <tt CLASS=literal>setPixels</tt>(int <tt CLASS=literal>x</tt>, int <tt CLASS=literal>y</tt>, int <tt CLASS=literal>w</tt>, int <tt CLASS=literal>h</tt>, ColorModel <tt CLASS=literal>model</tt>, int[] <tt CLASS=literal>pixels</tt>, int <tt CLASS=literal>off</tt>, int <tt CLASS=literal>scansize</tt>);
            public void <tt CLASS=literal>setPixels</tt>'u'//<tt CLASS=literal> Overrides ReplicateScaleFilter</tt>
}
</PRE>
</DIV>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-21-SECT-1.2">Hierarchy:</A></h3>

<P CLASS=para>
<tt CLASS=literal>Object->ImageFilter(ImageConsumer, Cloneable)->
ReplicateScaleFilter->AreaAveragingScaleFilter</tt>

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch20_30.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch21_02.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>java.awt.event.WindowListener (JDK 1.1)</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>java.awt.image.ColorModel (JDK 1.0)</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
